package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jb.repair.R;
import com.jb.repair.dao.Db;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.model.SbbjModel;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.util.List;

/**
 * 备品备件
 */
public class SpareEquipActivtity extends BaseActivity {

    private TitleView titleView;
    private Db db;
    private LinearLayout ll_bpbj;
    private int i = -1;

    private InfoTextView tv_name,tv_id,tv_version,tv_factory,tv_price,tv_ration,tv_stock,tv_data,tv_main_param,tv_location,tv_remark;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bpbjxq);
        initviews();
        db = Db.getInstance(this);

        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "备品备件");
        Log.e("1231", form_CODE);
        SbbjModel sbbjModel = (SbbjModel) getIntent().getSerializableExtra("bjsb");
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");
        tv_name.setText(sbbjModel.spar_name);

        //判断是否可见
        String visible = db.getVisible(form_CODE, "spar_code_editable");
        Log.e("1231","spar_code_editable ="+visible);
        if (!visible.equals("T")){
            tv_id.setVisibility(View.GONE);
        }
        tv_id.setText(sbbjModel.spar_code_editable);

        tv_version.setText(sbbjModel.model);
        tv_factory.setText(sbbjModel.producer);
        tv_price.setText(sbbjModel.price_editable);

        String visible1 = db.getVisible(form_CODE, "quota_num_editable");
        Log.e("1231","quota_num_editable ="+visible1);
        if (!visible1.equals("T")){
            tv_ration.setVisibility(View.GONE);
        }
        tv_ration.setText(sbbjModel.quota_num_editable);

        tv_stock.setText(sbbjModel.stock_num);
//没有生产日期
//        int i = sbbjModel.scrq.lastIndexOf(".");
//        if (-1 != i){
        //单位
        tv_data.setText(sbbjModel.unit);
//        }else{
//            tv_data.setText(sbbjModel.scrq);
//        }
        String visible3 = db.getVisible(form_CODE, "tech_parameter_editable");
        Log.e("1231","tech_parameter_editable"+visible3);
        if(!visible3.equals("T")){
            tv_main_param.setVisibility(View.GONE);
        }
        tv_main_param.setText(sbbjModel.tech_parameter_editable);

        String visible5 = db.getVisible(form_CODE, "storage_location_editable");
        Log.e("1231","stoage_location_editable"+visible5);
        if(!visible5.equals("T")){
            tv_location.setVisibility(View.GONE);
        }
        tv_location.setText(sbbjModel.storage_location_editable);
        tv_remark.setText(sbbjModel.note);

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        Log.e("1231","id_zhuanye = "+id_zhuanye);
        for (SbbdzdyModel model1:sbbdzdyModelList ){
            if(model1.is_show_eqbook.equals("T")){
                InfoTextView infoTextView = new InfoTextView(this);
                ll_bpbj.addView(infoTextView);
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);
                //设置value的值

                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, sbbjModel.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);

//                String zdyzdValue = db.getZdyzdValue(model1.guid, sbbjModel.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
//-------------------------------------------------------------------------------------------------------


    }

    private void initviews(){
        titleView = (TitleView) this.findViewById(R.id.titleView);

        tv_name = (InfoTextView) findViewById(R.id.tv_name);
        tv_id = (InfoTextView) findViewById(R.id.tv_id);
        tv_version = (InfoTextView) findViewById(R.id.tv_version);
        tv_factory = (InfoTextView) findViewById(R.id.tv_factory);
        tv_price = (InfoTextView) findViewById(R.id.tv_price);
        tv_ration = (InfoTextView) findViewById(R.id.tv_ration);
        tv_stock = (InfoTextView) findViewById(R.id.tv_stock);
        tv_data = (InfoTextView) findViewById(R.id.tv_data);
        tv_main_param = (InfoTextView) findViewById(R.id.tv_main_param);
        tv_location = (InfoTextView) findViewById(R.id.tv_location);
        tv_remark = (InfoTextView) findViewById(R.id.tv_remark);
        ll_bpbj = (LinearLayout) findViewById(R.id.ll_bpbj);

        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener(){
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });
    }
}
